import {
  getList,
  addList,
  getDel,
  editList,
  editRole,
  getRoleList
} from "./service";
import { message } from "antd";
export default {
  namespace: "mgtname",
  state: {
    selected: [],
    visibleDTM: false,
    page: {
      list: [],
      pageSize: 10,
      pageNum: 1,
      total: 0
    },
    allRole: {
      checkedKeys: [],
      treeData: []
    },
    loading: true
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // eslint-disable-line
    }
  },

  effects: {
    *getList(
      {
        payload: { pageNum, pageSize }
      },
      { call, put }
    ) {
      yield put({ type: "changeLoading", payload: true });
      try {
        const res = yield call(getList, { pageNum, pageSize });
        yield put({
          type: "save",
          payload: {
            page: {
              list: res.pageData,
              pageSize: res.pageSize,
              pageNum: res.pageNum,
              total: res.total
            }
          }
        });
      } catch (e) {
        message.error(e.message);
      }
      yield put({ type: "changeLoading", payload: false });
    },

    *add({ payload }, { call, put, select }) {
      yield put({ type: "changeLoading", payload: true });
      try {
        yield call(addList, payload);
        const total = yield select(state => state.mgtname.page.total);
        const pageSize = yield select(state => state.mgtname.page.pageSize);
        const pageNum = Math.ceil((total + 1) / pageSize);
        const res = yield call(getList, { pageNum, pageSize });
        yield put({
          type: "save",
          payload: {
            page: {
              list: res.pageData,
              pageSize: res.pageSize,
              pageNum: res.pageNum,
              total: res.total
            }
          }
        });
      } catch (e) {
        message.error(e.message);
      }

      yield put({ type: "changeLoading", payload: false });
    },

    *delete({ callback, payload }, { call, put, select }) {
      yield put({ type: "changeLoading", payload: true });
      try {
        yield call(getDel, payload.selectedRowKeys);
        const pageNum = yield select(state => state.mgtname.page.pageNum);
        const pageSize = yield select(state => state.mgtname.page.pageSize);
        const res = yield call(getList, { pageNum, pageSize });
        yield put({
          type: "save",
          payload: {
            page: {
              list: res.pageData,
              pageSize: res.pageSize,
              pageNum: res.pageNum,
              total: res.total
            }
          }
        });
        if(callback) {
          callback(res);
        };
      } catch (e) {
        message.error(e.message);
      }

      yield put({ type: "changeLoading", payload: false });
    },

    *edit({ payload }, { call, put, select }) {
      yield put({ type: "changeLoading", payload: true });
      try {
        yield call(editList, payload);
      } catch (e) {
        message.error(e.message);
      }
      const pageNum = yield select(state => state.mgtname.page.pageNum);
      const pageSize = yield select(state => state.mgtname.page.pageSize);
      const res = yield call(getList, { payload: pageNum, pageSize });
      yield put({
        type: "save",
        payload: {
          page: {
            list: res.pageData,
            pageSize: res.pageSize,
            pageNum: res.pageNum,
            total: res.total
          }
        }
      });
      yield put({ type: "changeLoading", payload: false });
    },

    /**
     * 获取用户角色分配情况
     */
    *getRoleList({ callback, payload }, { call, put }) {
      yield put({ type: "changeLoading", payload: true });
      try {
        const res = yield call(getRoleList, { adminId: payload });
        if (callback) {
          callback(res);
        }
      } catch (e) {
        message.error(e.message);
      }
      yield put({ type: "changeLoading", payload: false });
    },

    *editRole({ payload }, { call, put, select }) {
      yield put({ type: "changeLoading", payload: true });
      try {
        yield call(editRole, payload);
        const pageNum = yield select(state => state.mgtname.page.pageNum);
        const pageSize = yield select(state => state.mgtname.page.pageSize);
        const res = yield call(getList, { pageNum, pageSize });
        yield put({
          type: "save",
          payload: {
            page: {
              list: res.pageData,
              pageSize: res.pageSize,
              pageNum: res.pageNum,
              total: res.total
            }
          }
        });
      } catch (e) {
        message.error(e.message);
      }
      yield put({ type: "changeLoading", payload: false });
    }
  },

  reducers: {
    changeLoading(state, { payload }) {
      return { ...state, loading: payload };
    },

    save(state, { payload }) {
      return { ...state, ...payload };
    }
  }
};
